// SetParametersDlg.cpp : implementation file
//

#include "stdafx.h"
#include "RMSServer.h"
#include "SetParametersDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSetParametersDlg dialog


CSetParametersDlg::CSetParametersDlg(CWnd* pParent)
	: CDialog(CSetParametersDlg::IDD, pParent)
	, m_seq(0)
	, m_packsize(0)
	, m_FecMode(0)
{
	//{{AFX_DATA_INIT(CSetParametersDlg)
	m_port = 777;
	m_password = _T("");
	m_bitRate = 35;
	m_nFPS = 5;
	m_addr = _T("");
	//}}AFX_DATA_INIT
}


void CSetParametersDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSetParametersDlg)
	DDX_Control(pDX, IDC_FRAMERATE, m_frameRate);
	DDX_Control(pDX, IDC_ADDRESS, m_ipAddress);
	DDX_Control(pDX, IDC_CODEC, m_codecs);
	DDX_Text(pDX, IDC_PORT, m_port);
	DDX_Text(pDX, IDC_PASSWORD, m_password);
	DDX_Text(pDX, IDC_BITRATE, m_bitRate);
	//}}AFX_DATA_MAP
	DDX_CBIndex(pDX, IDC_COMBO1, m_seq);
	DDX_CBIndex(pDX, IDC_COMBO2, m_packsize);
	DDX_CBIndex(pDX, IDC_COMBO3, m_FecMode);
}


BEGIN_MESSAGE_MAP(CSetParametersDlg, CDialog)
	//{{AFX_MSG_MAP(CSetParametersDlg)
	//}}AFX_MSG_MAP
#if _MSC_VER <= 1200 // before equal than Visual C++ 6.0
	ON_CBN_SELCHANGE(IDC_COMBO3, CSetParametersDlg::OnCbnSelchangeCombo3)
#else
	ON_CBN_SELCHANGE(IDC_COMBO3, &CSetParametersDlg::OnCbnSelchangeCombo3)
#endif
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSetParametersDlg message handlers

extern int EnableFEC;
extern int FECparam1;
extern int FECparam2;


BOOL CSetParametersDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	for( POSITION pos = m_list.GetHeadPosition(); pos != NULL; )
	{
		m_ipAddress.AddString(m_list.GetNext(pos));
	}
	m_ipAddress.SelectString(-1, m_addr);

	m_codecs.AddString("WG-Codec"); //charith 22-05-2015
	m_codecs.SelectString(-1,"WG-Codec"); //charith 22-05-2015
	m_codecs.AddString("Theora-Codec"); //charith 22-05-2015
	//m_codecs.SelectString(-1,"RMS-Codec");

	TCHAR buf[30];
	for( int i = 1; i < 31; i++ )
	{
		_stprintf(buf, _T("%i"), i);
		m_frameRate.AddString(buf);
	}
	m_frameRate.SetCurSel(m_nFPS-1);

    m_FecMode = EnableFEC;
    m_seq = FECparam2-1; if (m_seq<0) m_seq=0;
    m_packsize = (FECparam1-9)/33-1;

	UpdateData(FALSE);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CSetParametersDlg::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData(TRUE);
	m_ipAddress.GetLBText(m_ipAddress.GetCurSel(), m_addr);
	m_nFPS = m_frameRate.GetCurSel() + 1;

    EnableFEC = m_FecMode;
    FECparam2 = m_seq +1;
    FECparam1 = (m_packsize+1)*33+9;

    REGISTRY_SaveInt(KEY_FEC_ENABLED, EnableFEC);
    REGISTRY_SaveInt(KEY_FEC_PARAM1, FECparam1);
    REGISTRY_SaveInt(KEY_FEC_PARAM2, FECparam2);

	CDialog::OnOK();
}

void CSetParametersDlg::PresetBitRate(int br) 
{
	m_bitRate = br;
}

void CSetParametersDlg::PresetPassword(CString password) 
{
	m_password = password;
}

void CSetParametersDlg::PresetPort(int port) 
{
	m_port = port;
}

void CSetParametersDlg::PresetFrameRate(int nfps) 
{
	m_nFPS = nfps;
// BUGFIX for DEBUG flag
	if (m_frameRate)
	{
	    m_frameRate.SetCurSel(nfps-1);
	}
}

void CSetParametersDlg::PresetIPAddress(CStringList* pList, CString curaddr)
{
	m_addr = curaddr;
	m_list.AddTail(pList);
}

int CSetParametersDlg::GetBitRate() 
{
    if (m_bitRate<10) PresetBitRate(10);
	return m_bitRate;
}

int CSetParametersDlg::GetFrameRate() 
{
	return m_nFPS;
}

CString CSetParametersDlg::GetPassword() 
{
	return m_password;
}

int CSetParametersDlg::GetPort() 
{
	return m_port;
}

CString CSetParametersDlg::GetIPAddress() 
{
	return m_addr;
}


void CSetParametersDlg::OnCbnSelchangeCombo3()
{
	// TODO: Add your control notification handler code here
}
